<template>
  <basic-container>
    <avue-crud :option="option"
               :table-loading="loading"
               :data="data"
               :page="page"
               :permission="permissionList"
               :before-open="beforeOpen"
               v-model="form"
               ref="crud"
               @row-update="rowUpdate"
               @row-save="rowSave"
               @row-del="rowDel"
               @search-change="searchChange"
               @search-reset="searchReset"
               @selection-change="selectionChange"
               @current-change="currentChange"
               @size-change="sizeChange"
               @on-load="onLoad"
               v-if="showMain"
    >
      <template slot="menuLeft">
        <el-button type="danger"
                   size="small"
                   icon="el-icon-delete"
                   plain
                   @click="handleDelete">删 除
        </el-button>
      </template>

      <template  slot="menuLeft">
        <el-button type="warning" icon="el-icon-back" size="small" @click.stop="goBack()">返回列表</el-button>
      </template>
    </avue-crud>
  </basic-container>
</template>

<script>
  import {getList, add, update, remove} from "@/api/service/jsJsnr";
  import {mapGetters} from "vuex";

  export default {
    props: {
      nrId: {
        type: String,
        default: '0'
      }, showMainView: {
        type: Function,
        default: null
      },appUserId:{
        type: String,
        default: '0'
      }
    },
    data() {
      return {
        showMain:true,
        show:false,
        form: {},
        query: {},
        loading: true,
        page: {
          pageSize: 10,
          currentPage: 1,
          total: 0
        },
        selectionList: [],
        aa:false,
        option: {
          height: 'auto',
          calcHeight: 210,
          searchShow: true,
          searchMenuSpan: 6,
          tip: false,
          border: true,
          index: true,
          viewBtn: true,
          selection: true,
          // menuType:'menu',
          // menuBtnTitle:'更多操作',
          column: [
            {
              label: "主键",
              prop: "id",
              rules: [{
                required: true,
                message: "请输入主键",
                trigger: "blur"
              }],
              hide:true,
              display:false
            },
            {
              label: "所获奖项",
              prop: "url",
              rules: [{
                required: true,
                message: "请选择所获奖项",
                trigger: "blur"
              }],
              type: 'upload',
              loadText: '附件上传中，请稍等',
              limit: 1,
              accept: "image/*",
              listType: 'picture-img',
              span: 24,
              propsHttp: {
                url: 'link',
                name: 'name',
                res: 'data'
              },
              action: '/api/localfiles/uploadPcFile',
              tip: '只能上传jpg/png文件，且不超过500kb',
              // hide: true
            },
          ]
        },
        data: []
      };
    },
    computed: {
      ...mapGetters(["permission","JSuserInfo"]),
      permissionList() {
        return {
          addBtn: true,//this.vaildData(this.permission.jkcsevaluate_add, false),
          viewBtn: this.vaildData(this.permission.jkcsevaluate_view, true),
          delBtn: this.vaildData(this.permission.jkcsevaluate_delete, true),
          editBtn: this.vaildData(this.permission.jkcsevaluate_edit, true)
        };
      },
      jsId() {
        return this.JSuserInfo.js_Id
      },
      ids() {
        let ids = [];
        this.selectionList.forEach(ele => {
          ids.push(ele.id);
        });
        return ids.join(",");
      }
    },
    methods: {
      goBack(){
        this.showMainView();
      },
      rowSave(row, done, loading) {
        row.id = row.id === '' ? '0' : row.id
        row.AppUserId = this.appUserId;
        row.parentId = this.nrId;
        row.tenant_id = '' //租户 id
        add(row).then(() => {
          done();
          this.onLoad(this.page);
          this.$message({
            type: "success",
            message: "操作成功!"
          });
        }, error => {
          window.console.log(error);
          loading();
        });
      },
      rowUpdate(row, index, done, loading) {
        row.AppUserId = this.appUserId;
        row.parentId = this.nrId;
        row.tenant_id = '' //租户 id

        update(row).then(() => {
          done();
          this.onLoad(this.page);
          this.$message({
            type: "success",
            message: "操作成功!"
          });
        }, error => {
          window.console.log(error);
          loading();
        });
      },
      rowDel(row) {
        this.$confirm("确定将选择数据删除?", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        })
          .then(() => {
            return remove({ids:row.id,AppUserId:this.appUserId});
          })
          .then(() => {
            this.onLoad(this.page);
            this.$message({
              type: "success",
              message: "操作成功!"
            });
          });
      },
      handleDelete() {
        if (this.selectionList.length === 0) {
          this.$message.warning("请选择至少一条数据");
          return;
        }
        this.$confirm("确定将选择数据删除?", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        })
          .then(() => {
            return remove({ids:this.ids,AppUserId:this.appUserId});
          })
          .then(() => {
            this.onLoad(this.page);
            this.$message({
              type: "success",
              message: "操作成功!"
            });
            this.$refs.crud.toggleSelection();
          });
      },
      beforeOpen(done, type) {
        if (["edit", "view"].includes(type)) {
          // getDetail(this.form.id).then(res => {
          //   this.form = res.data.data;
          // });
        }
        done();
      },
      searchReset() {
        this.query = {};
        this.onLoad(this.page);
      },
      searchChange(params, done) {
        this.query = params;
        this.page.currentPage = 1;
        this.onLoad(this.page, params);
        done();
      },
      selectionChange(list) {
        this.selectionList = list;
      },
      selectionClear() {
        this.selectionList = [];
        this.$refs.crud.toggleSelection();
      },
      currentChange(currentPage){
        this.page.currentPage = currentPage;
      },
      sizeChange(pageSize){
        this.page.pageSize = pageSize;
      },
      onLoad(page, params = {}) {
        this.loading = true;
        params.id = this.nrId;
        getList(page.currentPage, page.pageSize, Object.assign(params, this.query)).then(res => {
          const data = res.data.data;
          this.page.total = data.totalItems;
          this.data = data.items;
          this.loading = false;
          this.selectionClear();
        });
      },
      showView(row){
        this.jsId = row.id;
        //this.$refs.eps.onLoad({currentPage:1,pageSize:10});
        this.showMain = false;
        this.show = true;
      },
      // showMainView(){
      //   this.showMain = true;
      //   this.show = false;
      // },
    }
  };
</script>

<style>
</style>
